<?php

class Files {

    public static $data;


    public static function import(){

    }

    public static function make_dir($dir){

        if( !is_dir($dir) ){
            mkdir($dir);
        }
    }

    public static function open($filename){

        try{
            $fp = fopen( $filename, 'r');

            $t = "";
            while ($line = fgets ($fp)) {
                $t .= $line;
            }

            self::$data = $t;

            fclose($fp);

            return $t;

        }catch (exception $e){
            return $e;
        }

    }


    public static function save($filename, $inp = ""){

        if($inp == ""){
            $inp = $this->data;
        }

        $fp = fopen($filename, 'w+');
        fwrite($fp, $inp);
        fclose($fp);
    }

	public function get_icon($file){

		$path_parts = pathinfo($file);

		switch($path_parts['extension']){
			case "xls" : case "xlsx" : return "xls.png";
			case "doc" : case "docx" : return "doc.png";
			case "ppt" : case "pptx" : return "ppt.png";
			case "ai" : return "ai.png";
			case "html" : case "htm" : case "php" : return "html.png";
			case "psd" : return "psd.png";
			case "pdf" : return "pdf.png";
			case "wmv" : case "wma" : return "wmv.png";
			case "rar" : case "zip" : return "zip.png";
			case "swf" : return "swf.png";
			case "txt" : return "txt.png";
			case "mp3" : return "mp3.png";
			case "jpg" : case "png" : return "jpg.png";

			default : "default.png";
		}
	}
	
	public function upload($file, $savefile = ""){

		if ($_FILES[$file]['name'] != ''){

			if ($_FILES[$file]['error'] > 0) {
				echo 'Problem: ';
				switch ($_FILES[$file]['error']){
					case 1:
						echo 'File exceeded upload_max_filesize';
						break;
					case 2:
						echo 'File exceeded max_file_size';
						echo 'please resize picture to less than 600K then try again';
						break;
					case 3:
						echo 'File only partially uploaded';
						break;
					case 4:
						echo 'No file uploaded';
						break;
				}
				return false;
			}

			if (is_uploaded_file($_FILES[$file]['tmp_name'])){

				if($savefile == ""){
					$path_info = pathinfo($_FILES[$file]['name']);
					$filename = time();
					$savefile = UPLOAD. $filename .".".$path_info['extension'];
					$destination = URL."upload/".$filename .".".$path_info['extension'];

					if(file_exists($savefile)){
						$filename = time()+1;
						$savefile = UPLOAD. $filename .".".$path_info['extension'];
						$destination = URL."upload/". $filename .".".$path_info['extension'];
					}
				}

				if (!move_uploaded_file($_FILES[$file]['tmp_name'], $savefile)){
					echo 'Problem: Could not move file to destination directory';
					return false;
				}else{

					return array($destination, $savefile);
				}
			}else{
				echo 'Problem: Possible file upload attack. Filename: '.$file;
				return false;
			}
			chmod($savefile, 0755);
		}

		return false;
	}

	public function downloadFile($file){
		$file_name = $file;
		$mime = 'application/force-download';
		header('Pragma: public');  // required
		header('Expires: 0');  // no cache
		header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
		header('Cache-Control: private',false);
		header('Content-Type: '.$mime);
		header('Content-Disposition: attachment; filename="'.basename($file_name).'"');
		header('Content-Transfer-Encoding: binary');
		header('Connection: close');
		readfile($file_name);  // push it out
		exit();
	}
}

?>